<?php
class Util {
	/**
	 * 得到随机数
	 *
	 * @param unknown_type $length
	 * @param unknown_type $mode
	 * @return unknown
	 */
	static function getRandCode ($length = 32, $mode = 0)
	{
		switch ($mode) {
			case '1':
				$str = '1234567890';
				break;
			case '2':
				$str = 'abcdefghijklmnopqrstuvwxyz';
				break;
			case '3':
				$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
				break;
			case '4':
				$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
				break;
			case '5':
				$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
				break;
			case '6':
				$str = 'abcdefghijklmnopqrstuvwxyz1234567890';
				break;
			default:
				$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
				break;
		}
		$result = '';
		$l = strlen($str);
		for($i = 0;$i < $length;$i ++){
			$num = rand(0, $l);
			$result .= $str[$num];
		}
		return $result;
	}

	/**
	 * 得到邮箱的主机信息
	 *
	 * @param unknown_type $email
	 */
	static function getEmailHost($email) {
		if (preg_match("/^(\w+([-+.']\w+)*)@(\w+(?:[-.]\w+)*(\.\w+([-.]\w+)*)*)$/", $email, $matches)) {
			$host = $matches[3];
			$hostName = substr($host, 0, strpos($host, '.'));
			$hostUrl = "mail.".$host;
			switch($host) {
				case "google.com":
					$hostUrl = "www.gmail.com";
					break;
				case "hotmail.com":
				case "live.cn":
				case "msn.com":
					$hostUrl = "mail.live.com";
					break;
			}
			return array("name"=>$hostName,"url"=>$hostUrl);
		}
		return NULL;
	}

	/**
	 * 存储图片的缩略图
	 *
	 * @param unknown_type $path 原图片路径
	 * @param unknown_type $newPath 保存的路冷静
	 * @param unknown_type $newWidth 新的宽度
	 * @param unknown_type $newHeight 新的高度 如果为NULL，表示只对宽度予以限制
	 */
	static function thumbPicture($path,$newPath,$newWidth, $newHeight=NULL) {
		list($oldWidth, $oldHeight, $type) = getimagesize($path);
		if ($newHeight) {	// 对高和宽都有所限制
			if ($oldWidth == $newWidth && $oldHeight == $newHeight) {
				copy($path, $newPath);
				return;
			}
			$oldRate = $oldWidth/$oldHeight;
			$newRate = $newWidth/$newHeight;
	
			$oldLeft = $oldTop = 0;
			if ($oldRate > $newRate) { // 原有宽高比大于现有宽高比，需要缩短原有宽度
				$_oldWidth = intval($oldHeight*$newRate);
				$oldLeft = ($oldWidth - $_oldWidth)/2;
				$oldWidth = $_oldWidth;
			} else {
				$_oldHeight = intval($oldWidth/$newRate);
				$oldTop = ($oldHeight - $_oldHeight)/2;
				$oldHeight = $_oldHeight;
			}
		} else {	// 只限制宽
			// 只有图片的宽度大于最大宽度时，才会进行剪裁
			if ($oldWidth <= $newWidth) return;
			
			$newHeight = intval($oldHeight * ($newWidth/$oldWidth));
			$oldLeft = $oldTop = 0;
		}
		
		switch($type){
			case 1:
				$img=imagecreatefromgif($path);
				break;
			case 2:
				$img=imagecreatefromjpeg($path);
				break;
			case 3:
				$img=imagecreatefrompng($path);
				break;
		}
		
		$im=imagecreatetruecolor($newWidth,$newHeight);
		$black=imagecolorallocate($im,255,255,255);
		imagefilledrectangle($im,0,0,$newWidth,$newHeight,$black);

		imagecopyresized($im,$img,0,0,$oldLeft,$oldTop,$newWidth,$newHeight,$oldWidth,$oldHeight);

		imagejpeg($im, $newPath);
		imagedestroy($im);
		imagedestroy($img);

		return $type;
	}
}
?>